package com.bcx.crm.java.collection.sortAlgorithm;

/**
 * Created by ys on 2018/3/1.
 * 两个有序数组，如何高效的合成一个有序数组
 */
public class TwoArraySort {
    public static void main(String[] args) {
        int[] a = new int[]{0,4,8,9,15,66};
        int[] b = new int[]{8,9,15,66,77,88,99};
        int[] ints = TwoArraySort.arraySort(a, b);
    }
    private static int[] arraySort(int[] a,int[] b){
        int [] c = new int[a.length+b.length];
        int i=0 , j=0 , k=0;
        while (i<a.length && j<b.length){
            if (a[i] < b[j]){
               c[k++] = a[i++];
            }else {
                c[k++] = b[j++];
            }
        }
        while (i<a.length){
            c[k++] = a[i++];
        }
        while (j<b.length){
            c[k++] = b[j++];
        }
        return c;
    }
}
